# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt

mnist = input_data.read_data_sets("G:\\code\\dataset\\mnist", one_hot=False)
# mnist_data = input_data.read_data_sets("G:\\code\\dataset", one_hot=True)#, source_url=http://yann.lecun.com/exdb/mnist/)

#选取训练集中的第 1 个图像的矩阵
mnist_one=mnist.train.images[0]

plt.subplot(121)
plt.imshow(mnist_one.reshape((28,28)), cmap=plt.cm.gray)

#输出图片的维度，结果是：(784,)
print(mnist_one.shape)

#因为原始的数据是长度是 784 向量，需要转换成 1*28*28*1 的矩阵。
mnist_one_image=mnist_one.reshape((1,28,28,1))

#输出矩阵的维度
print(mnist_one_image.shape)

#滤波器参数
filter_array=np.asarray([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]])

#滤波器维度
print(filter_array.shape)

#调整滤波器维度
filter_tensor=filter_array.reshape((3,3,1,1))

#卷机操作
conv_image_tensor=tf.nn.convolution(mnist_one_image,filters=filter_tensor.astype(np.float32), padding="SAME")
# conv_image_tensor=tf.nn.convolution(mnist_one_image,padding="SAME")#,filter=tf.cast(filter_tensor, dtype=tf.float32)

#返回的张量维度
print(conv_image_tensor.shape)

#调整为二维图片
conv_images=tf.reshape(conv_image_tensor,[28,28])
#获得张量的值
with tf.compat.v1.Session() as sess:
    conv_image = sess.run(conv_images)
plt.subplot(122)

#使用 matplotlib 输出为图片
plt.imshow(conv_image, cmap=plt.cm.gray)

plt.show()